<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var product_id = '{{.product.Id}}';
var typeid = '{{.product.TypeId}}';

var columns = [];
columns.push({"bSortable": false, "sTitle":"门店",'width':'10%', render:function (data, type, full, meta) {
        return full.department.Name;
}});
columns.push({"bSortable": false, "sTitle":"公共", render:function (data, type, full, meta) {
        return full.is_public == 1 ?'<span class="am-badge am-badge-warning">公共</span>' : '-';
}});
switch (typeid) {
    case '10':
        columns.push({"data" : "sph_min", "bSortable": false, "sTitle":"球镜下限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
        }});
        columns.push({"data" : "sph_max", "bSortable": false, "sTitle":"球镜上限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "cyl_min", "bSortable": false, "sTitle":"柱镜下限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "cyl_max", "bSortable": false, "sTitle":"柱镜上限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "custom_trade_price", "bSortable": false, "sTitle":"定制批发价"});
        columns.push({"data" : "custom_suggested_price", "bSortable": false, "sTitle":"定制销售价"});
        break;
    case '20':
        columns.push({"data" : "sph_min", "bSortable": false, "sTitle":"球镜下限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "sph_max", "bSortable": false, "sTitle":"球镜上限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "cyl_min", "bSortable": false, "sTitle":"柱镜下限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "cyl_max", "bSortable": false, "sTitle":"柱镜上限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "custom_trade_price", "bSortable": false, "sTitle":"定制批发价"});
        columns.push({"data" : "custom_suggested_price", "bSortable": false, "sTitle":"定制销售价"});
        break;
    case '30':
        columns.push({"bSortable": false, "sTitle":"型号",render:function (data, type, full, meta) {
                return full.snd.model;
        }});
        columns.push({"bSortable": false, "sTitle":"色号",render:function (data, type, full, meta) {
                return full.snd.color;
        }});
        columns.push({"bSortable": false, "sTitle":"尺寸",render:function (data, type, full, meta) {
                return full.snd.size;
        }});
        columns.push({"bSortable": false, "sTitle":"框型",render:function (data, type, full, meta) {
                return full.snd.genre;
        }});
        break;
    case '40':
        columns.push({"data" : "sph_min", "bSortable": false, "sTitle":"球镜下限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "sph_max", "bSortable": false, "sTitle":"球镜上限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "cyl_min", "bSortable": false, "sTitle":"ADD下限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        columns.push({"data" : "cyl_max", "bSortable": false, "sTitle":"ADD上限",render:function (data, type, full, meta) {
                return data > 0 ? '+' + formatnumber(data) : formatnumber(data);
            }});
        break;
    case '41':
        columns.push({"bSortable": false, "sTitle":"型号",render:function (data, type, full, meta) {
                return full.snd.model;
            }});
        columns.push({"bSortable": false, "sTitle":"色号",render:function (data, type, full, meta) {
                return full.snd.color;
            }});
        columns.push({"bSortable": false, "sTitle":"尺寸",render:function (data, type, full, meta) {
                return full.snd.size;
            }});
        columns.push({"bSortable": false, "sTitle":"框型",render:function (data, type, full, meta) {
                return full.snd.genre;
            }});
        columns.push({"bSortable": false, "sTitle":"功效",render:function (data, type, full, meta) {
                return full.snd.function_info;
            }});
        break;

}
columns.push({"data" : "trade_price", "bSortable": false, "sTitle":"批发价"});
columns.push({"data" : "suggested_price", "bSortable": false, "sTitle":"销售价"});

columns.push({
    "mDataProp": "Action",
    "bSortable": false,
    "sWidth":"10%",
    "sTitle":"操作",
    "render": function ( data, type, full, meta ) {
        btns = '<a class="am-icon-edit" href="javascript:;" onclick="actionEdit('+ full.id +');return false;"> 编辑</a>';
        btns += '&nbsp;&nbsp;<a class="am-icon-close" href="javascript:;" onclick="actionDelete('+ full.id +');return false;"> 删除</a>';
        return btns;
    }
});

var tables = $('#dataTables').DataTable({
    "sAjaxSource" : location.href
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        var params = checkQueryTableData(aoData);
        $('#search_key').val() ? params.filter = $('#search_key').val() : '';
        $('#DepartmentId').val() ? params.department = Number($('#DepartmentId').val()) : '';

        $.post(sSource, JSON.stringify(params),function(resp) {
            fnCallback(resp);
            if(typeid == '50' || typeid == '60') {
                if(resp.iTotalDisplayRecords > 0) {
                    $('#insert-btn').hide();
                } else {
                    $('#insert-btn').show();
                }
            }
        },'json')
    }
    ,order:[]
    ,columns: columns
    ,"aLengthMenu":[20, 50, 100, 200]
    ,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
});

function actionDelete(id) {
    layer.confirm("确定要删除该价格吗？", function (index) {
        layer.close(index);
        $.post('{{ urlfor "ProductsController.PriceDelete" }}', {'price_id':id}, function (res) {
            if(res.code == 1) {
                layer.alert(res.msg);return false;
            }
            dialogReback();
        });
    });
}

//新增
function actionInsert() {
    var uri = '{{ urlfor "ProductsController.PriceAdd" }}?product_id='+product_id;
    layer.open({
        type: 2,
        title:'新增价格',
        shadeClose: false,
        shade: 0.2,
        maxmin: false,
        shift: 1,
        area: ['90%', '80%'],
        content: uri
    });
}

//编辑
function actionEdit(id) {
    var uri = '{{ urlfor "ProductsController.PriceEdit" }}?product_id='+product_id+'&price_id='+id;
    layer.open({
        type: 2,
        title:'编辑价格',
        shadeClose: false,
        shade: 0.2,
        maxmin: false,
        shift: 1,
        area: ['750px', '450px'],
        content: uri
    });
}

$('#DepartmentId').change(function () {
    tables.ajax.reload();
});

function dialogReback() {
    tables.ajax.reload();
}
//设置门店列表
get_manage_departments(function (d) {
    var opt = [];
    if (d.length > 0) {
        opt.push('<option value="">请选择</option>');
        $.each(d, function (idx, val) {
            opt.push('<option value="' + val.Id + '">' + val.Name + '</option>');
        });
    }
    $('#DepartmentId').html(opt.join('')).val({{.department.Id}});
});
</script>